#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int elem;
    struct Node* next;
}node;

node* initQueue()
{
    node* queue = (node*)malloc(sizeof(node));
    if(queue == NULL)
    {
        printf("空间开辟失败...\n");
        return queue;
    }
    queue->next = NULL;
    return queue;
}

node* push(node* tail, int elem)
{
    node* newElem = (node*)malloc(sizeof(node));
    if(newElem == NULL)
    {
        printf("空间开辟失败...\n");
        return tail;
    }
    newElem->elem = elem;
    newElem->next = NULL;
    tail->next = newElem;
    tail = newElem;
    return tail;
}

node* pop(node* top, node* tail)
{
    node* temp;
    if(top->next == NULL)
    {
        printf("队空...\n");
        return top;
    }
    temp = top->next;
    printf("%d 出队...\n", temp->elem);
    top->next = temp->next;
    if(temp == tail)
    {
        tail = top;
    }
    free(temp);
    temp = NULL;
    return tail;
}

int main()
{
    node *queue,*top,*tail;
    queue=top=tail=initQueue();//创建头结点
    //向链队列中添加结点，使用尾插法添加的同时，队尾指针需要指向链表的最后一个元素
    tail=push(tail, 1);
    tail=push(tail, 2);
    tail=push(tail, 3);
    tail=push(tail, 4);
    //入队完成，所有数据元素开始出队列
    tail=pop(top, tail);
    tail=pop(top, tail);
    tail=pop(top, tail);
    tail=pop(top, tail);
    tail=pop(top, tail);
    return 0;
}



